package cj.web.tel.controllers;

import java.text.ParseException;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.login.controllers.BaseController;
import cj.web.tel.constants.GroupPeopleTypeEnum;
import cj.web.tel.domain.FloorInfor;
import cj.web.tel.domain.GroupDep;
import cj.web.tel.domain.GroupInfor;
import cj.web.tel.domain.GroupOffices;
import cj.web.tel.domain.GroupPeople;
import cj.web.tel.service.FloorInforService;
import cj.web.tel.service.GroupDepService;
import cj.web.tel.service.GroupInforService;
import cj.web.tel.service.GroupOfficesService;
import cj.web.tel.service.GroupPeopleService;

@Path("/groupPeople")
public class GroupPeopleController extends BaseController{

	private static Logger logger = Logger.getLogger(GroupPeopleController.class.getName());
	
	@Autowired
	private GroupPeopleService groupPeopleService;
	@Autowired
	private GroupDepService groupDepService;
	@Autowired
	private GroupOfficesService groupOfficesService;
	@Autowired
	private FloorInforService floorInforService;
	@Autowired
	private GroupInforService groupInforService;
	
	
	@Get("show")
	public String show(Model model,GroupPeople groupPeople) {
		return "/views/tel/groupPeopleList.vm";
	}
	
	@Post("getList")
	public String queryList(GroupPeople groupPeople) {
	
		//当前页  
		groupPeople.setStart((groupPeople.getPage()-1)*groupPeople.getRows());
		groupPeople.setLength(groupPeople.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = groupPeopleService.query(groupPeople);
		
		List<GroupPeople> objs = (List<GroupPeople>) rep.get("groupPeoplelis");
		for(GroupPeople obj:objs){
			obj.setTypeName(GroupPeopleTypeEnum.getName(obj.getType()));
		}
		
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model,@Param("depId") int depId){
		GroupDep groupDep = groupDepService.queryById(depId);
		GroupInfor groupInfor = groupInforService.queryById(groupDep.getGroupId());
		model.add("groupPeopleTypeEnum",EnumSet.allOf(GroupPeopleTypeEnum.class));
		model.add("depId", depId);
		model.add("depName", groupDep.getName());
		model.add("groupId", groupInfor.getId());
		model.add("groupName", groupInfor.getName());
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		
		List<GroupOffices> officelis = groupOfficesService.queryAllById(depId);
		model.add("officelis", officelis);
		
		return "/views/tel/groupPeopleDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,GroupPeople groupPeople,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		groupPeople.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		groupPeople.setCreateAt(DateUtils.getNowTime());
		groupPeople.setCreateUser(returnSessionObj(request).getAccount());
		groupPeople.setUpdateAt(DateUtils.getNowTime());
		groupPeople.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			groupPeopleService.create(groupPeople);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) throws ParseException
	{
		model.add("groupPeopleTypeEnum",EnumSet.allOf(GroupPeopleTypeEnum.class));
		GroupPeople groupPeople = groupPeopleService.queryById(id);
		model.add("groupPeople", groupPeople);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		List<GroupOffices> officelis = groupOfficesService.queryAllById(groupPeople.getDepId());
		model.add("officelis", officelis);
		
		return "/views/tel/groupPeopleEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,GroupPeople groupPeople,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		groupPeople.setUpdateAt(DateUtils.getNowTime());
		groupPeople.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			groupPeopleService.update(groupPeople);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			groupPeopleService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	
	//-----------------------groupDep
	@Post("getGroupDepList")
	public String queryGroupDepList(GroupDep groupDep) {
		JSONObject jo = new JSONObject();
		//当前页  
		groupDep.setStart((groupDep.getPage()-1)*groupDep.getRows());
		groupDep.setLength(groupDep.getRows());
		Map rep = groupDepService.query(groupDep);
		List<GroupDep> objs = (List<GroupDep>) rep.get("groupDeplis");
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createGroupDepPage")
	public String createGroupDepPage(Model model,@Param("groupId")int groupId){
		model.add("groupId", groupId);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		return "/views/tel/groupDepDetail.vm";
	}
	
	@Post("createGroupDep")
	public String createGroupDep(Model model,GroupDep groupDep,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		groupDep.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		groupDep.setCreateUser(returnSessionObj(request).getAccount());
		groupDep.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			groupDepService.create(groupDep);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editGroupDep")
	public String editGroupDep(Model model,@Param("id") int id){
		GroupDep obj = groupDepService.queryById(id);
		model.add("groupDep", obj);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		return "/views/tel/groupDepEdit.vm";
	}
	
	@Post("updateGroupDep")
	public String updateGroupDep(Model model,GroupDep groupDep,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		groupDep.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			groupDepService.update(groupDep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteGroupDepById")
	public String deleteGroupDepById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			groupDepService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	//-----------------------groupOffices
	@Post("getGroupOfficesList")
	public String queryGroupOfficesList(GroupOffices groupOffices) {
		JSONObject jo = new JSONObject();
		//当前页  
		groupOffices.setStart((groupOffices.getPage()-1)*groupOffices.getRows());
		groupOffices.setLength(groupOffices.getRows());
		Map rep = groupOfficesService.query(groupOffices);
		List<GroupOffices> objs = (List<GroupOffices>) rep.get("groupOfficeslis");
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createGroupOfficesPage")
	public String createGroupOfficesPage(Model model,@Param("groupId")int groupId,
			@Param("depId")int depId){
		model.add("groupId", groupId);
		model.add("depId", depId);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		return "/views/tel/groupOfficesDetail.vm";
	}
	
	@Post("createGroupOffices")
	public String createGroupOffices(Model model,GroupOffices groupOffices,
			HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		groupOffices.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		groupOffices.setCreateUser(returnSessionObj(request).getAccount());
		groupOffices.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			groupOfficesService.create(groupOffices);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editGroupOffices")
	public String editGroupOffices(Model model,@Param("id") int id){
		GroupOffices obj = groupOfficesService.queryById(id);
		model.add("groupOffices", obj);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		return "/views/tel/groupOfficesEdit.vm";
	}
	
	@Post("updateGroupOffices")
	public String updateGroupOffices(Model model,GroupOffices groupOffices,
			HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		groupOffices.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			groupOfficesService.update(groupOffices);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteGroupOfficesById")
	public String deleteGroupOfficesById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			groupOfficesService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}